﻿
//@javascript chat written By Diego Stefanon diego_2171@yahoo.it ... asp.net & C# programmer @
    var m_idChat = -1;
    var m_chatName='';
    var m_UserId = '';
    var m_userName = '';
    var m_lastMsgId= 1;
    var m_DivDest = null;
    var m_DivUtenti = null;
    var m_TextNewMsg = null;
    var m_Color = 'Black';
    var m_fontStyle = '';
    var m_divMsgFont = null;
    var m_Testo;
    var m_Timer1; var m_Timer2; var m_Timer3;
    var m_fontDiv = null;
    var m_colorDiv = null;
    var m_curDate = new Date();
    var m_stileNormal="color:Black;font-style:normal;text-decoration:none;font-size:normal;";
    var m_arrayStili= {"color":"Black", "font-style":"normal", "font-weight":"normal", "text-decoration":"none","font-size":"normal" };;
    var m_arrayStiliNormali = { "color": "Black", "font-style": "normal", "font-weight": "normal", "text-decoration": "none","font-size":"normal" }; ;

    function setPickColor(colore) {
        //alert(colore);
        m_colorDiv = document.getElementById('ButtonInvia');
        m_colorDiv.style.color = colore;
        m_Color =colore;

        m_arrayStili["color"] = colore;
        m_fontStyle = getStyleMsg();
        
        m_TextNewMsg.focus();
    } //fm
    
    //imposta il font style 
    function setPickColor2(obj,fontS) {
        //alert(colore);
        //m_colorDiv = document.getElementById('showColor_Div');

        var t = fontS.split(':');
        
        if (t[0] == "font-size") {
            document.getElementById('fs1').style.backgroundColor = "Transparent";
            document.getElementById('fs2').style.backgroundColor = "Transparent";
            document.getElementById('fs3').style.backgroundColor = "Transparent";
        }
        
        var sel = 0;
        if (obj.style.backgroundColor == 'Yellow') {
            sel = 1;
            obj.style.backgroundColor = 'Transparent'
        }
        else
            obj.style.backgroundColor = 'Yellow';
        
        if (sel == 1) //deselezionare
        {
            m_arrayStili[t[0]] = m_arrayStiliNormali[t[0]];
            
        }
        if (sel == 0) //selezionare
        {
            m_arrayStili[t[0]] = t[1];
        }

        m_fontStyle = getStyleMsg();
        //alert(m_fontStyle );     
    
        m_TextNewMsg.focus();
    } //fm

    function getStyleMsg() {
          return  "color:" + m_arrayStili['color'] + ";font-style:" + m_arrayStili["font-style"] +
         ";font-weight:" + m_arrayStili["font-weight"] + ";text-decoration:" + m_arrayStili["text-decoration"] +
         ";font-size:" + m_arrayStili["font-size"] +";";

    }//fm

    function msgFont(msg){
             
        m_divMsgFont = document.getElementById('div_fontMsg');
        //m_divMsgFont.style.visibility = 'visible';
        m_divMsgFont.innerHTML = msg;

    }//fm
   
    function setIdChat(id_Chat,chatName) {

        m_idChat = id_Chat;
        m_chatName = chatName;
        //alert(idChat ); 
    }//fm
    
    
    //chiamata diretta a webSer in pagina
    function getMessaggi() {
        //var ctrl = document.getElementById(src);
        //var dest = document.getElementById('div_chat');
       
        PageMethods.GetMessaggi(m_idChat,m_lastMsgId , CallSuccess, CallFailed, m_DivDest );
    }

    function setMessaggi(msg) {

        if (m_idChat < 1)
        { alert('Selezionare una chat,poi premere Start ... '); return; }

        if (msg != null)
            m_Testo = msg;
        else
            m_Testo = m_TextNewMsg.value;
            
        if (m_Testo == "") return;
        m_Testo = rightTrim(m_Testo);
        
        //alert(m_Testo +"  " );
        m_TextNewMsg.value  = "";
        m_TextNewMsg.focus();
        //var style = "color:" + m_Color + ";" + m_fontStyle + ";";
        getStyleMsg();
        try {
            PageMethods.SetMessaggi(m_Testo, m_idChat, m_UserId, m_userName,m_fontStyle   , CallSuccessUpload, CallFailed, m_DivDest);
        }
        catch (ex)
        { alert(ex)} 
    } //fm

    function startChat(userId,userName) {

        if (m_idChat < 1)
        { alert('Selezionare una chat, prego... '); return; }
           
        m_DivDest = document.getElementById('div_chat');
        m_TextNewMsg = document.getElementById('TextNewMsg');
        m_DivUtenti = document.getElementById('div_Utenti');

        m_DivUtenti.innerHTML = "";

        
        m_DivDest.innerHTML = "<b><font color='Green'>In chat " + m_chatName + "  -  " + 
        m_curDate.toTimeString() +  "</font></b>"; 
        m_TextNewMsg.value="";
                 
        m_UserId = userId;
        m_userName = userName;

        //alert(m_UserId + "  " + m_userName + "  " + m_idChat); 
        
        m_TextNewMsg.focus();
        PageMethods.getIndiceMessaggio(m_idChat, CallOKindice, CallFailed);
         //-->
        delObsoleti();
    } //fm

    function getUtentiConnessi() {

        PageMethods.getUtentiConnessi(m_idChat, CallOKUtenti, CallFailed);       
    }//fm 

    function Termina(userName) {

        clearTimeout(m_Timer1);
        clearTimeout(m_Timer2);
        clearTimeout(m_Timer3);
        PageMethods.terminaChat(m_idChat, m_userName, CallOKTermina, CallFailed, m_DivDest);

    } //fm

    function delObsoleti() {
        PageMethods.deleteObsoleti(CallOKdelObsoleti, CallFailed, m_DivDest);
    
    }//fm

    function CallOKdelObsoleti(res, destCtrl) {
        m_Timer3 = setTimeout('delObsoleti();', 10000);
    } //fm
    
    function CallOKTermina(res, destCtrl) {

        m_DivDest.innerHTML = "<b><font color='Green'>Chiusa chat " + m_chatName + "  -  " +
         m_curDate.toTimeString() + "</font></b>"; 
        
    } //fm

    function CallOKindice(res, destCtrl) {
        m_lastMsgId = res;
        //alert(m_lastMsgId);
        PageMethods.startChat(m_idChat, m_UserId, m_userName, CallOKStartChat, CallFailed, m_DivUtenti);

    } //fm

    function CallOKUtenti(res, destCtrl) {

        var temp;
        m_DivUtenti.innerHTML = "";
        
        
       alert("CAll utenti "+  res[0]) 
        for (var i = 0; i < res.length; i++) {
            temp = res[i].split("<|$|>");
            m_DivUtenti.innerHTML += temp[0] + "<br/>";
            //m_lastMsgId = temp[0];
        }
        m_Timer2 = setTimeout('getUtentiConnessi();', 4000);

    } //fm

    function CallOKStartChat(res, destCtrl) {

        var temp;
        for (var i = 0; i < res.length; i++) {
            temp = res[i].split("<|$|>");
            m_DivUtenti.innerHTML += temp[0] +"<br/>";
            //m_lastMsgId = temp[0];
        }
           
        getMessaggi();
    } //fm
    
    function CallSuccessUpload(res, destCtrl)
    {
        
    }

    // set the destination textbox value with the ContactName
    function CallSuccess(res, destCtrl) {
        //alert("ok res "+ res);
        //var dest = document.getElementById(destCtrl);
        //alert(dest);
        //dest.value += res;
        //alert('rit  ' + res);
        try {
            var temp;
            for (var i = 0; i < res.length; i++) {
                temp = res[i].split("<|$|>");
                destCtrl.innerHTML += temp[1];
                m_lastMsgId = temp[0];
            }
           }
        catch (ex) { alert(ex) }

        destCtrl.scrollTop = destCtrl.offsetHeight;
        
        m_Timer1 = setTimeout('getMessaggi();', 1800);
    }//fm

    // alert message on some failure
    function CallFailed(res, destCtrl) {
        alert("Errore... "+res.get_message());
    }


    function trim(stringa) {
        while (stringa.substring(0, 1) == ' ') {
            stringa = stringa.substring(1, stringa.length);
        }
        while (stringa.substring(stringa.length - 1, stringa.length) == ' ') {
            stringa = stringa.substring(0, stringa.length - 1);
        }
        return stringa;
    } //fm

    function rightTrim(stringa) {
        while (stringa.substring(stringa.length - 1, stringa.length) == ' ') {
            stringa = stringa.substring(0, stringa.length - 1);
        }
        return stringa;
    }//fm
    
    

    
    
    